package com.homegoods.area.ui.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.contrarywind.view.WheelView;
import com.homegoods.area.R;
import com.homegoods.area.ui.model.ProvinceBean;
import com.kedll.library.util.MyUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author QiQi
 * @ClassName: GetOptionsUtil
 * @date 2018/10/6
 * @description
 */
public class GetOptionsUtil {

    private OptionsPickerView pvOptions;
    private List<ProvinceBean> options1Items = new ArrayList<>();
    private List<List<String>> options2Items = new ArrayList<>();
    private Disposable disposable;

    private OnCallBack onCallBack;

    public void setOnCallBack(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }

//    public void initJsonData(Context context) {
//        disposable = Observable.just("province.json")
//                .map(s -> new GetJsonDataUtil().getJson(context, s))
//                .map(s -> JSONArray.parseArray(s, ProvinceBean.class))
//                .flatMap(provinceBeans -> {
//                    options1Items = provinceBeans;
//                    return Observable.fromIterable(provinceBeans);
//                })
//                .map(provinceBean -> {
//                    List<String> cityList = new ArrayList<>();
//                    for (int i = 0; i < provinceBean.getCity().size(); i++) {
//                        String cityName = provinceBean.getCity().get(i).getName();
//                        cityList.add(cityName);
//                    }
//                    return cityList;
//                })
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .doOnComplete(() -> initOptions(context))
//                .subscribe(cityList -> options2Items.add(cityList));
//
//
//    }

    private void initOptions(Context context) {
        pvOptions = new OptionsPickerBuilder(context, (options1, options2, options3, v1) -> {
            String tx = options1Items.get(options1).getPickerViewText() +
                    options2Items.get(options1).get(options2);
            if (onCallBack != null) {
                onCallBack.onResult(tx);
            }

        })
                .setLayoutRes(R.layout.designer_pickerview_custom_options, view -> {
                    final TextView tvSubmit = view.findViewById(R.id.tv_finish);
                    final TextView tvCancel = view.findViewById(R.id.tv_cancel);
                    tvSubmit.setOnClickListener(v -> {
                        pvOptions.returnData();
                        pvOptions.dismiss();
                    });

                    tvCancel.setOnClickListener(v -> pvOptions.dismiss());
                })
                .setDividerColor(ContextCompat.getColor(context, R.color.color_eeeeee))
                .setBgColor(Color.WHITE)
                .setBackgroundId(0x00000000)
                .isDialog(true)
                .setDividerType(WheelView.DividerType.FILL)
                .setTextColorOut(ContextCompat.getColor(context, R.color.color_aaaaaa))
                .setTextColorCenter(ContextCompat.getColor(context, R.color.color_333333))
                .setContentTextSize(14)
                .build();
        pvOptions.setKeyBackCancelable(false);
        pvOptions.setPicker(options1Items, options2Items);

        Dialog mDialog = pvOptions.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvOptions.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                //修改动画样式
                dialogWindow.setWindowAnimations(R.style.picker_view_slide_anim);
                //改成Bottom,底部显示
                dialogWindow.setGravity(Gravity.BOTTOM);
            }
        }
    }

    public void onDestroy() {
        MyUtils.dispose(disposable);
    }

    public void show() {
        if (null != pvOptions) {
            pvOptions.show();
        }
    }

    public interface OnCallBack {
        void onResult(String result);
    }
}
